/*
 * Copyright 2010 Biz-e (info@biz-e.nl)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.site.client;

import nl.biz_e.aclclient.shared.AclAuthenticator;
import nl.biz_e.gwt.baselib.ui.client.menu.IMenuItem;
import nl.biz_e.gwt.baselib.ui.client.menu.MenuItem;
import nl.biz_e.gwt.baselib.ui.client.menu.IMenuItem.MenuItemClickedListener;
import nl.biz_e.gwtappengine.lib.client.AbstractApplication;
import nl.biz_e.site.client.editor.EditorPanel;
import nl.biz_e.site.client.editor.EditorStrings;
import nl.biz_e.site.client.editor.NewsEditPanel;
import nl.biz_e.site.client.editor.UsersPanel;
import nl.biz_e.site.client.editor.VersionsPanel;

import com.google.gwt.core.client.GWT;

/**
 * Adds some simple CMS functionalities to the Biz-e site
 * 
 * @author Joost A. Bloemsma
 */
public class ContentEditor extends AbstractApplication implements
        MenuItemClickedListener {

    private final EditorStrings strings = (EditorStrings) GWT
            .create(EditorStrings.class);

    private final MenuItem mnuContent;
    private final MenuItem mnuVersions;
    private final MenuItem mnuNews;
    private final MenuItem mnuUsers;

    private EditorPanel editor;
    private UsersPanel users;
    private VersionsPanel versions;
    private NewsEditPanel news;

    /**
     * Constructs a new ContentEditor instance
     */
    public ContentEditor() {
        super("Biz-e Content Editor");
        mnuContent = new MenuItem(strings.content(), this);
        mnuVersions = new MenuItem(strings.versions(), this);
        mnuNews = new MenuItem(strings.news(), this);
        mnuUsers = new MenuItem(strings.users(), this);
        this.setShowPreLoginPanel(false);
        setUseGoogleLogin(true, "/editor.html");
        this.addMenuItem(mnuContent);
        this.addMenuItem(mnuVersions);
        this.addMenuItem(mnuNews);
        // this.addMenuItem(mnuUsers);
        this.setAuthenticator(new AclAuthenticator());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onLoggedIn() {
        setSelected(mnuContent);
        showPanel(getEditor());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void menuItemClicked(IMenuItem item) {
        if (item.isSelected())
            return;

        if (item == mnuContent) {
            showPanel(getEditor());
        } else if (item == mnuVersions) {
            showPanel(getVersions());
        } else if (item == mnuUsers) {
            showPanel(getUsers());
        } else if (item == mnuNews) {
            showPanel(getNews());
        } else {
            return;
        }
        setSelected(item);
    }

    private void setSelected(IMenuItem menuItem) {
        for (IMenuItem item : getMenuItems()) {
            item.setSelected(item == menuItem);
        }
    }

    private EditorPanel getEditor() {
        if (editor == null) {
            editor = new EditorPanel();
        }
        return editor;
    }

    private UsersPanel getUsers() {
        if (users == null) {
            users = new UsersPanel();
        }
        return users;
    }

    private VersionsPanel getVersions() {
        if (versions == null) {
            versions = new VersionsPanel();
        }
        return versions;
    }

    private NewsEditPanel getNews() {
        if (news == null) {
            news = new NewsEditPanel();
        }
        return news;
    }
}
